package me.zhulu.dubbo.group.impl;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;

/**
 * Created with IntelliJ IDEA.
 * User: herosea
 * Date: 14-7-3
 * Time: 上午11:21
 * To change this template use File | Settings | File Templates.
 */
public class DubboFactory {

    private ApplicationConfig application;
    private RegistryConfig registry;

    public <T>T get(String group, Class<T> clazz) {
        ReferenceConfig<T> reference = new ReferenceConfig<T>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
        reference.setInterface(clazz);
        reference.setGroup(group);
        reference.setCheck(Boolean.FALSE); // 必须设置，否则provider不存在时多次创建reference造成资源泄露，拖慢系统
        return reference.get();
    }

    public ApplicationConfig getApplication() {
        return application;
    }

    public void setApplication(ApplicationConfig application) {
        this.application = application;
    }

    public RegistryConfig getRegistry() {
        return registry;
    }

    public void setRegistry(RegistryConfig registry) {
        this.registry = registry;
    }
}
